package com.hk.admin.module.system.service;

import com.hk.admin.common.constant.GlobalConstants;
import com.hk.admin.common.constant.GlobalEnum;
import com.hk.admin.common.constant.system.UserEnum;
import com.hk.admin.common.base.dto.message.Message;
import com.hk.admin.common.dto.system.user.UserDTO;
import com.hk.admin.common.model.system.UserModel;
import com.hk.admin.common.base.service.BaseService;
import com.hk.admin.module.system.dao.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class LoginService extends BaseService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserRepository userRepository;

    /**
     * 登录
     *
     * @return
     */
    public ResponseEntity<Message> login(String phoneNumber) {
        logger.debug("-----------------登录开始----------------");
        UserModel user = userRepository.findByPhoneNumberAndDeleted(phoneNumber, GlobalEnum.DELETED.NO.getKey());
        // 不存在
        if (user == null) {
            logger.debug("-----------------用户不存在----------------");
            return error(GlobalConstants.LOGIN_ERROR);
        }
        // 帐号被禁用
        if (user.getAccountStatus() == null || user.getAccountStatus() == UserEnum.ACCOUNT_STATUS.DISABLE.getKey()) {
            logger.debug("-----------------用户帐号禁用----------------");
            return error(GlobalConstants.ACCOUNT_UNAVAILABLE);
        }

        UserDTO systemUserDTO = new UserDTO(user);
        logger.debug("-----------------登录结束----------------");
        return success(systemUserDTO);
    }

}
